﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;
using System.Data.Common;
using System.Globalization;
using System.Web.Script.Services;
using System.Web.Security;
using System.Web.Services;
using System.Collections.ObjectModel;
using System.Text;
using System.Configuration;

using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace TractRecord.Administration
{
    public partial class Logs : TractRecordBasePage
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static void ClearLog()
        {
            Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>("TractRecordDB");
            DbCommand cmd = db.GetStoredProcCommand("ClearLogs");
            db.ExecuteNonQuery(cmd);
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static object ReadLog(int? numRows, int? page, string sortField, string sortOrder, bool isSearch, string searchField, string searchString, string searchOper)
        {
            Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>("TractRecordDB");

            string[] SearchArray = searchString.Split(',');

            for (int i = 0; i < SearchArray.Length; i++)
            {
                SearchArray[i] = "'" + SearchArray[i] + "'";
            }

            string quotedSearchString = string.Join(",", SearchArray);

            DbCommand cmd = db.GetStoredProcCommand("ReadLog", new object[] { page, numRows, sortField, sortOrder, searchField, quotedSearchString, searchOper });
            DataSet ds = db.ExecuteDataSet(cmd);

            DataView dv = new DataView(ds.Tables[0]);

            DataTable dt = dv.ToTable();
            
            int pageIndex = page ?? 1; //--- current page
            int pageSize = numRows ?? 10; //--- number of rows to show per page
            int totalRecords = dt.Rows.Count > 0 ? (int)ds.Tables[0].Rows[0]["RowCount"] : 0; //--- number of total items from query
            int totalPages = (int)Math.Ceiling((decimal)totalRecords / (decimal)pageSize); //--- number of pages


            var rowlist = (from DataRow dr in dt.Rows
                           select new
                           {
                                LogId = dr["LogId"].ToString(),
                                   EventId = dr["EventId"].ToString(),
                                   Priority = dr["Priority"].ToString(),
                                   Severity = dr["Severity"].ToString(),
                                   Title = dr["Title"].ToString(),
                                   Timestamp = dr["Timestamp"].ToString(),
                                   MachineName = dr["MachineName"].ToString(),
                                   ProcessId = dr["ProcessId"].ToString(),
                                   Message = dr["Message"].ToString(),
                                   FormattedMessage = dr["FormattedMessage"].ToString(),
                                   CategoryName = dr["CategoryName"].ToString()
                               
                           }
                           ).ToArray();
            



            var value = new
            {
                totalpages = totalPages, //--- number of pages
                page = pageIndex, //--- current opage
                totalrecords = totalRecords, //--- total items
                rows = (from row in rowlist
                        select new {
                            id = row.LogId,
                            LogId = row.LogId,
                            EventId = row.EventId,
                            Priority = row.Priority,
                            Severity = row.Severity,
                            Title = row.Title,
                            Timestamp = row.Timestamp,
                            MachineName = row.MachineName,
                            ProcessId = row.ProcessId,
                            Message = row.Message,
                            FormattedMessage = row.FormattedMessage,
                            CategoryName = row.CategoryName
                        }
                ).ToArray()

            };

            return value;
        }
    }
}